<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>线路数据管理</title>
    <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
    <script src="js/vue.js"></script>
    <script src="element-ui/lib/index.js"></script>
    <script src="js/axios-0.18.0.js"></script>
</head>
<body>
<div id="div">
    <b style="color: red; font-size: 20px;">线路列表</b>
    <div style="float: right;">
        <el-button type="primary" @click="showAdd">添加线路</el-button>
    </div>
    <el-table :data="tableData">
        <el-table-column prop="rid" label="线路id" >
        </el-table-column>
        <el-table-column prop="rname" label="线路名称" >
        </el-table-column>
        <el-table-column prop="routeIntroduce" label="线路介绍">
        </el-table-column>
        <el-table-column prop="price" label="价格">
        </el-table-column>
        <el-table-column label="操作" >
            <template slot-scope="scope">
                <img :src="scope.row.rimage" style="width:100px;height:50px;"/>
            </template>
        </el-table-column>

        <el-table-column label="操作" >
            <template slot-scope="props">
                <el-button type="warning" @click="showEdit(props.row)">编辑</el-button>
                <el-button type="danger" @click="deleteRoute(props.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <!--
        分页组件
          @size-change： 当改变每页条数时触发的函数
          @current-change：当改变页码时触发的函数
          current-page ：默认的页码
          :page-sizes：每页条数选择框中显示的值
          :page-size : 默认的每页条数
          layout： 分页组件的布局
              total（总条数）, sizes(每页条数), prev（上一页）, pager(所有的页码), next(下一页), jumper（跳转页码）
          :total: 总条数
    -->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pagination.currentPage"
            :page-sizes="[5,10,15, 20]"
            :page-size="pagination.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pagination.total">
    </el-pagination>

    <!--
          :model="formData": 关联数据
          :rules: 关联校验规则
          ref： 在获取表单对象时使用
      -->
  <el-dialog title="添加旅游线路信息" :visible.sync="dialogTableVisible4add" @close="resetForm('addForm')">
        <el-form :model="formData"  ref="addForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="线路名称">
                <el-input v-model="formData.rname"></el-input>
            </el-form-item>
            <el-form-item label="线路介绍">
                <el-input v-model="formData.routeIntroduce"></el-input>
            </el-form-item>
            <el-form-item label="线路价格" >
                <el-input v-model="formData.price" ></el-input>
            </el-form-item>
            <el-form-item label="线路分类" >
                <el-select v-model="formData.cid" placeholder="请选择">
                    <el-option
                            v-for="item in options"
                            :key="item.cid"
                            :label="item.cname"
                            :value="item.cid">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item align="right">
                <el-button type="primary" @click="saveRoute()">添加</el-button>
                <el-button @click="resetForm('addForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <!--
      &lt;!&ndash;
            :model="formData": 关联数据
            :rules: 关联校验规则
            ref： 在获取表单对象时使用
        &ndash;&gt;
    -->

 <el-dialog title="修改旅游线路信息" :visible.sync="dialogTableVisible4edit" @close="resetForm('editForm')">

        <el-form :model="editFormData"  ref="editForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="线路名称">
                <el-input v-model="editFormData.rname"></el-input>
                <el-input v-model="editFormData.rid" type="hidden"></el-input>
            </el-form-item>
            <el-form-item label="线路介绍">
                <el-input v-model="editFormData.routeIntroduce"></el-input>
            </el-form-item>
            <el-form-item label="线路价格" >
                <el-input v-model="editFormData.price" ></el-input>
            </el-form-item>
            <el-form-item label="线路分类" >
                <el-select v-model="editFormData.cid" placeholder="请选择">
                    <el-option
                        v-for="item in options"
                        :key="item.cid"
                        :label="item.cname"
                        :value="item.cid">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item align="right">
                <el-button type="warning" @click="updateRoute()">修改</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</div>
</body>
<script>
    new Vue({
        el: "#div",
        data: {
            dialogTableVisible4add: false,  //添加窗口显示状态
            dialogTableVisible4edit: false, //编辑窗口显示状态
            formData: {},//添加表单的数据
            editFormData: {},//编辑表单的数据
            tableData: [],//表格数据
            pagination: {
                currentPage: 1, //当前页
                pageSize: 5,    //每页显示条数
                total: 0        //总条数
            },
            options: []

        },
        methods: {
            findByPage(){
                axios.get("/travel_crud_practice/RouteServlet?action=selectRoutesByPage",{
                    params:{
                        pageNum : this.pagination.currentPage,
                        pageSize : this.pagination.pageSize
                    }
                }).then(resp=>{
                    if(resp.data != null){
                        this.tableData = resp.data.list;
                        this.pagination.total = resp.data.total;
                    }
                })

            },
            handleSizeChange(val) {
                this.pagination.pageSize = val;
            },
            handleCurrentChange(val) {
                this.pagination.currentPage = val;
                this.findByPage();
            },
            showAdd(){
                this.dialogTableVisible4add = true;
                axios.get("/travel_crud_practice/CategoryServlet?action=selecAllCategorys").then(resp=>{
                    this.options = resp.data;
                })
            },
            showEdit(obj){
                this.dialogTableVisible4edit=true;
                this.editFormData = obj;
                axios.get("/travel_crud_practice/CategoryServlet?action=selecAllCategorys").then(resp=>{
                    this.options = resp.data;
                })
/*                axios.post("/travel_crud_practice/RouteServlet?action=findRouteByid",obj).then(resp=>{
                    //this.editFormData = resp.data;
                    options = resp.data.category
                })*/

            },
            updateRoute(){
                axios.post("/travel_crud_practice/RouteServlet?action=udpateRoute",this.editFormData).then(resp=>{
                    if(resp.data == "ok"){
                        this.dialogTableVisible4edit = false;
                        this.findByPage();
                    }
                })
            },
            saveRoute(){
                axios.post("/travel_crud_practice/RouteServlet?action=saveRoute",this.formData).then(resp=>{
                    if(resp.data=="ok"){
                        this.dialogTableVisible4add = false;
                        this.findByPage();
                    }
                })
            },
            deleteRoute(obj){
                axios.post("/travel_crud_practice/RouteServlet?action=delReoute",obj).then(resp=>{
                    if(resp.data == "ok"){
                        this.findByPage();
                    }
                })
            },
            resetForm(){

            }
        },
        created() {
            this.findByPage();
        }
    });
</script>
</html>